﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace JEdwonTune.Lib.Reflect {
    public static class RefHelper {
        public static BizDllEO ParseBizAssembly(String filePath) {
            if (String.IsNullOrEmpty(filePath)) {
                 throw new Exception("请先选择程序集文件");
            }
            Assembly assem = Assembly.LoadFrom(filePath);
            if (assem == null) {
                throw new Exception("无法解析程序集，请确定该程序集有效");
            }
            String[] resFiles = assem.GetManifestResourceNames();
            BizDllEO dll = new BizDllEO();
            dll.Assem = assem;
            dll.Entities = new List<EntityEO>();
            Type[] types = assem.GetTypes();
            foreach (Type t in types) {
                if (!t.IsClass || !t.IsPublic) continue;
                EntityEO eo = new EntityEO();
                Type it = t.GetInterface("Cn.Edwon.Biz.IModel");
                eo.ImplModel = (it == null) ? "" : "Yes";
                eo.Type = t;
                eo.AssemblyName = assem.FullName.Split(',')[0];
                eo.HbmFile = "";
                foreach (String r in resFiles) {
                    if (r.IndexOf(t.Name) != -1) {
                        eo.HbmFile = r;
                        break;
                    }
                }
                dll.Entities.Add(eo);
            }
            return dll;
        }
        public static String ParseAssemblyInfo(String filePath) {
            if (String.IsNullOrEmpty(filePath)) {
                throw new Exception("请先选择程序集文件");
            }
            Assembly assem = Assembly.LoadFile(filePath);
            if (assem == null) {
                throw new Exception("无法打开程序集文件(" + filePath + ")，请确定该程序集有效");
            }
            if (assem.ManifestModule == null) {
                throw new Exception("缺少程序集清单模块，请确定该程序集有效");
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(assem.FullName);
            sb.Append("\n#CodeBase : "+assem.CodeBase);
            if(assem.EntryPoint==null)
                sb.Append("\n#EntryPoint : 未定义");
            else
                sb.Append("\n#EntryPoint : " + assem.EntryPoint.Name);
            sb.Append("\n#Global : " + assem.GlobalAssemblyCache);
            sb.Append("\n#HostContext : " + assem.HostContext);
            sb.Append("\n#ImageRuntimeVersion : " + assem.ImageRuntimeVersion);
            sb.Append("\n#Location : " + assem.Location);
            sb.Append("\n#ReflectionOnly : " + assem.ReflectionOnly);
            sb.Append("\n#EscapedCodeBase : " + assem.EscapedCodeBase);
            Module[] modules = assem.GetModules();
            String[] resFiles = assem.GetManifestResourceNames();
            sb.Append("\n---- 资源文件 ---------");
            for (int i = 0; i < resFiles.Length; i++) {
                sb.Append("\n@ " + resFiles[i]);
            }
            Module mm = assem.ManifestModule;
            Type[] types = assem.GetTypes();
            sb.Append("\n---- 类成员 ---------");
            for (int i = 0; i < types.Length; i++) {
                sb.Append("\n@ " + types[i].IsPublic);
                sb.Append(", " + types[i].FullName);
                sb.Append(", " + types[i].IsClass);
            }
            return sb.ToString();
        }
    }
}
